﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;

namespace logicunivesityinventorystore.Department
{
    public partial class GenerateRequisitionForm : System.Web.UI.Page
    {
        static DataAccessLayer.RetrivalListDetail detail;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            BindGridView();

         
        }

        private void BindGridView()
        {
            gvRetrivalDetails.DataSource = new RetrivalListManager().getRetrivalList();
            gvRetrivalDetails.DataBind();
        }
        
        protected void gvRetrivalDetails_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                
                gvItemDetails.DataSource = new RetrivalListManager().getRetrivalDetails(e.CommandArgument.ToString());
                gvItemDetails.DataBind();
            }
        }

        protected void btnApply_Click(object sender, EventArgs e)
        {

            Button btn = (Button)sender;
            if (btn.Text == "Apply")
            {
                foreach (GridViewRow row in gvItemDetails.Rows)
                {
                    string itemCode = row.Cells[1].Text;
                    string retrivalListID = row.Cells[0].Text;
                    TextBox txtQuantity = (TextBox)row.FindControl("txtActualQty");
                    int actualQty = Convert.ToInt32(txtQuantity.Text);
                    new RetrivalListFacade().UpdateActualQty(retrivalListID, itemCode, actualQty);
                }

                gvItemDetails.DataBind();
                BindGridView();
                if (gvRetrivalDetails.Rows.Count == 0)
                {
                    btnApply.Text = "Generate Disbursement List";
                }
            }
            else
            {
                Response.Redirect("GenerateDisbursementForm.aspx");
            }
        }

    }
}